package android.slc.appbase.ui.widget.spinner

import android.slc.box.core.controller.CustomTextFormatter
import android.slc.box.core.property.DataChangeListener
import javafx.beans.property.SimpleIntegerProperty
import javafx.scene.control.Spinner

class SpinnerNumberWrapper(
    private val spinner: Spinner<Int>,
    private val min: Int = 0,
    private val max: Int = 10000,
) {

    var currentValue: Int = 0

    val valueProperty = SimpleIntegerProperty()

    /**
     * 应用
     */
    fun applyControl() {
        //绑定排序
        this.spinner.editor.text = currentValue.toString()
        this.spinner.editor.textFormatter = CustomTextFormatter(CustomTextFormatter.OnChangeListener { change ->
            try {
                val number = change.text.toInt()
                number in min..max
            } catch (e: Exception) {
                false
            }
        })
        this.spinner.editor.textProperty().addListener(DataChangeListener {
            if (it != null) {
                try {
                    valueProperty.value = it.toInt()
                } catch (e: Exception) {

                }
            }
        })
        this.valueProperty.addListener(DataChangeListener {
            this.spinner.editor.text = it.toString()
        })
    }
}